package ${packagePath};

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springcloud.common.vo.DatePageVO;
<#-- import List -->
<#list importList as importStr>
${importStr}
</#list>
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;


/**
 * ServiceImpl
 *
 * @author by fast-code
 * @E-mail liaofuxing@outlook.com
 * @date ${generateDate}
 */
@Service
public class ${className} implements ${entityService} {

    @Autowired
    private ${entityDao} ${entityDaoParam};


    /**
     * 根据id查询 ${entity}
     */
    @Override
    public ${entity} findSystemUserById(Integer id) {
        return ${entityDaoParam}.findById(id).orElse(null);
    }


    /**
     * 查询用户列表
     *
     * @return List<${entity}>
     */
    @Override
    public List<${entity}> findAll() {
        return ${entityDaoParam}.findAll();
    }

<#if needPagination = true>
    /**
     * 用户分页查询
     *
     * @param ${entityDtoParam} dto
     * @return
     */
    public DatePageVO<${entityVo}> find${entity}Page(${entityDto} ${entityDtoParam}) {
        Pageable pageable = PageRequest.of(${entityDtoParam}.getPage() - 1, ${entityDtoParam}.getPageSize(), Sort.Direction.ASC, "id");

        Specification<${entity}> specification = (Specification<${entity}>) (root, criteriaQuery, criteriaBuilder) -> {
        // 分页条件组装
        List<Predicate> list = new ArrayList<>();

<#list pageQueryConditionFieldList as pageQueryConditionField>
    <#if pageQueryConditionField.paginationQueryCondition = true >
        if (!StringUtils.isEmpty(${entityDtoParam}.get${pageQueryConditionField.javaGetterORSetterColumnName}())) {
            list.add(criteriaBuilder.equal(root.get("${pageQueryConditionField.javaHumpColumnName}").as(String.class), ${entityDtoParam}.get${pageQueryConditionField.javaGetterORSetterColumnName}()));
        }
    </#if>
</#list>
        return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };

        Page<${entity}> ${entityParam}Page = ${entityDaoParam}.findAll(specification, pageable);

        List<${entityVo}> ${entityVoParam}List = BeanCopyUtil.copyListProperties(${entityParam}Page.getContent(), ${entityVo}::new);
        return new DatePageVO<>(${entityParam}Page.getTotalElements(), ${entityVoParam}List);
    }
</#if>

    /**
     * 新增 ${entity}
     *
     * @param ${entityDtoParam} dto
     *
     */
    @Transactional
    @Override
    public void add${entity}(${entityDto} ${entityDtoParam}) {
        ${entity} ${entityParam} = new ${entity}();
        BeanCopyUtil.copyProperties(${entityDtoParam}, ${entityParam});
        ${entityDaoParam}.save(${entityParam});
    }

    /**
    * 修改 ${entity}
    *
    * @param ${entityDtoParam} dto
    *
    */
    @Override
    public void edit${entity}(${entityDto} ${entityDtoParam}) {
        Optional<${entity}> byId = ${entityDaoParam}.findById(${entityDtoParam}.getId());
        ${entity} ${entityDtoParam}DB = byId.orElse(new ${entity}());
        BeanCopyUtil.copyProperties(${entityDtoParam}, ${entityDtoParam}DB);
        ${entityDaoParam}.save(${entityDtoParam}DB);
     }

}